#pragma once

#include <gofcl_dac_ctl.h>

typedef enum {
	DAC_TRIG_TIMER5 = 0,
	DAC_TRIG_TIMER2 = 1,
	DAC_TRIG_TIMER6 = 2,
	DAC_TRIG_TIMER4 = 3,
	DAC_TRIG_TIMER1 = 4,
	DAC_TRIG_TIMER3 = 5,
	DAC_TRIG_EXTI_LINE_9 = 6,
	DAC_TRIG_SOFTWARE = 7,
} dac_trig_e;

typedef enum {
	DAC_NOISE_DISABLE = 0,
	DAC_NOISE_LFSR = 1,
	DAC_NOISE_TRIANGLE = 2,
} dac_noise_wave_mode_e;

typedef enum {
	DAC_NOISE_WAVE_BIT_WIDTH_1 = 0,
	DAC_NOISE_WAVE_BIT_WIDTH_2 = 1,
	DAC_NOISE_WAVE_BIT_WIDTH_3 = 2,
	DAC_NOISE_WAVE_BIT_WIDTH_4 = 3,
	DAC_NOISE_WAVE_BIT_WIDTH_5 = 4,
	DAC_NOISE_WAVE_BIT_WIDTH_6 = 5,
	DAC_NOISE_WAVE_BIT_WIDTH_7 = 6,
	DAC_NOISE_WAVE_BIT_WIDTH_8 = 7,
	DAC_NOISE_WAVE_BIT_WIDTH_9 = 8,
	DAC_NOISE_WAVE_BIT_WIDTH_10 = 9,
	DAC_NOISE_WAVE_BIT_WIDTH_11 = 10,
	DAC_NOISE_WAVE_BIT_WIDTH_12 = 11,
} dac_noise_wave_bit_width_e;

typedef struct {
	u16_s dac_en : 1;
	u16_s buf_dis : 1;
	u16_s trig_en : 1;
	__attribute__((packed)) dac_trig_e trig_sel : 3;
	__attribute__((packed)) dac_noise_wave_mode_e noise_wave_mode : 2;
	__attribute__((packed))
	dac_noise_wave_bit_width_e noise_wave_bit_width : 4;
	u16_s dma_en : 1;
	u16_s : 3;
} dac_ctl_one_s;

_Static_assert(sizeof(dac_ctl_one_s) == 2,
	       "Struct dac_ctl_one_s defined error!");

typedef struct {
	dac_ctl_one_s dac_ctl_a[2];
} dac_ctl_expl_s;

_Static_assert(sizeof(dac_ctl_expl_s) == 4,
	       "Struct dac_ctl_expl_s defined error!");

static inline void dac_setup(dac_ctl_expl_s ctl) {
	union {
		dac_ctl_expl_s e;
		dac_ctl_s c;
	} cu = {
		.e = ctl,
	};
	dac_wreg_ctl_s(cu.c);
}
